<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor,configSelectData} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,sumF, getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				initValue:{
					applyPayMoneyStr:"",
					paidMoneyStr:"",
					posId: "",
					instId: ""
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				formdata:{
					applyPayMoney:"",
					paidMoney:""
				},
				itemCacheKey:'fin-bx-expenses-data-info-item-cache',
				checkedit:false
			}
		},
		watch:{
			"formdata.applyPayMoney":{
				handler(newValue,oldValue){
					let autoform =  this.$refs.autoform
					if(autoform){
						autoform.applyPayMoneyStr = (newValue || 0) + "元"
					}else{
						this.initValue.applyPayMoneyStr = (newValue || 0) + "元"
					}
				}
			},
			"formdata.paidMoney":{
				handler(newValue,oldValue){
					let autoform =  this.$refs.autoform
					if(autoform){
						autoform.paidMoneyStr = (newValue || 0) + "元"
					}else{
						this.initValue.paidMoneyStr = (newValue || 0) + "元"
					}
				}
			},
			formdata:{
				handler(newValue,oldValue){
					let _that = this.$refs.autoform
					_that.formdata.sumCosts = sumF(
						[
							this.formdata.longCosts,
							this.formdata.longCosts1,
							this.formdata.shortCosts,
							this.formdata.stayCosts,
							this.formdata.eatCosts,
							this.formdata.subsidyCosts,
							this.formdata.tollCosts,
							this.formdata.oilCosts,
							this.formdata.otherCosts
						]
					)
				},
				deep:true
			},
		},
		methods: {
			onSelectBusinessForm(item){
				this.getItemNum(item.code)
			},
			
			getItemNum(code){
				let _that = this
				_that.$refs.autoform.changeEditor("title2", "itemNum", "link","../../public/pageSearch/pageSearch")
				_that.$refs.autoform.changeEditor("title2", "itemNum", "linkParam", {
					other:JSON.stringify({
						businessFormCode:code,
					}),
					namefield:"itemNum",
					notefield:"userName",
					extrafield:"posName",
					searchfield:"q",
					initload:true,
					url:"tpay/findParamList",
					cachename:this.itemCacheKey
				})
			},
			clearBusinessForm(){
				this.$refs.autoform.clear("title2","businessFormCode")
				this.clearItem()
			},
			verifyItemNum(){
				if(!this.formdata.businessFormCode){
					return "请选择数据来源类型"
				}
				return true
			},
			clearItem(){
				let formdata = this.$refs.autoform.formdata
				formdata.itemNum = ""
				formdata.payCateName = ""
				formdata.userName = ""
				formdata.userId = ""
				formdata.posId = ""
				formdata.posName = ""
				formdata.instId = ""
				formdata.instName = ""
				formdata.applyPayMoneyStr = ""
				formdata.applyPayMoney = ""
				formdata.paidMoney = ""
				formdata.paidMoneyStr = ""
				formdata.remark = ""
				formdata.payee = ""
				formdata.bank = ""
				formdata.card = ""
				formdata.applyPayDate = ""
			},
			onSelectPayType(item) {
				let _that = this;
				if(item.code == "37848"){
					_that.$refs.autoform.changeEditor("title2","bank","required",true)
					_that.$refs.autoform.changeEditor("title2","card","required",true)
				}else{
					_that.$refs.autoform.changeEditor("title2","bank","required",false)
					_that.$refs.autoform.changeEditor("title2","card","required",false)
				}
			},
			clearPayType(){
				let _that = this;
				_that.$refs.autoform.clear("title2","payTypeCode")
				_that.$refs.autoform.changeEditor("title2","bank","required",false)
				_that.$refs.autoform.changeEditor("title2","card","required",false)
			},
			initLoad(){
				let _that = this
				//数据来源类型
				var pro1 = _that.om.request('tpay/findBusinessFormCodeList');
				//加载付款方式
				var pro2 = _that.om.request('dictZero/getListByCode', { code: "37846" });
						
				Promise.all([pro1, pro2]).then(res => {
					_that.$refs.autoform.changeEditor("title2", "businessFormCode", "data", configSelectData(res[0],"code","businessfun"))
					_that.$refs.autoform.initSelectIndex("title2", "businessFormCode")
					
					_that.$refs.autoform.changeEditor("title2", "payTypeCode", "data", configSelectData(res[1], "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("title2", "payTypeCode")
					
					this.getItemNum(_that.formdata.businessFormCode)
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "119_1")
								if(_that.formdata.fileList) {
									var tempFileList = []
									var fileUrlList = _that.formdata.fileList.map(t=>{
										tempFileList.push(t.fileSaveName)
										return t.fileSaveName
									})
									changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
									changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
								}
								_that.initLoad()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.itemCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							for(var i in d){
								if(formdata.hasOwnProperty(i) && d[i] != null && d[i] != undefined){
									formdata[i] = d[i]
								}
							}
							_that.$refs.autoform.initSelectIndex('title2','payTypeCode')
							uni.removeStorage({
								key: _that.itemCacheKey
							});
						}
					}
				});
				
			}
		}
	}
</script>

<style>

</style>
